#include "PreviewDialog.h"

PreviewDialog::PreviewDialog(const QString &title, const QString &summary, QWidget *parent)
    : QDialog(parent), coverImage()
{
    setWindowTitle("文章预览与封面设置");
    setFixedSize(1280, 100);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    blogItem = new BlogItemWidget(this, title, summary);
    mainLayout->addWidget(blogItem);

    selectImageButton = new QPushButton("选择封面图片", this);
    connect(selectImageButton, &QPushButton::clicked, this, &PreviewDialog::selectCoverImage);

    tagComboBox = new QComboBox(this);
    tagComboBox->addItem("理学工学农学", QVariant::fromValue(QString("sea")));
    tagComboBox->addItem("计算机", QVariant::fromValue(QString("cs")));
    tagComboBox->addItem("经济管理", QVariant::fromValue(QString("em")));
    tagComboBox->addItem("外语", QVariant::fromValue(QString("fl")));
    tagComboBox->addItem("音乐与艺术", QVariant::fromValue(QString("ma")));
    tagComboBox->addItem("心理学", QVariant::fromValue(QString("psy")));
    tagComboBox->addItem("文史哲法", QVariant::fromValue(QString("hum")));
    tagComboBox->addItem("医学与保健", QVariant::fromValue(QString("mh")));

    confirmButton = new QPushButton("确认发布", this);
    connect(confirmButton, &QPushButton::clicked, this, &QDialog::accept);

    selectImageButton->setStyleSheet(R"(
        QPushButton {
            background-color: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #4da6ff, stop:1 #0078ff);
            color: white;
            font-weight: bold;
            border-radius: 6px;
            padding: 8px;
        }
        QPushButton:hover {
            background-color: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #6bbfff, stop:1 #0095ff);
        }
    )");

    tagComboBox->setStyleSheet(R"(
        QComboBox {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #f9f9f9, stop:1 #e0e0e0);
            border: 1px solid #bbbbbb;
            border-radius: 6px;
            padding: 5px;
            font-size: 14px;
        }
        QComboBox:hover {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #ffffff, stop:1 #d9d9d9);
        }
        QComboBox:focus {
            border: 1px solid #8abeff;
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #ffffff, stop:1 #eef6ff);
        }
        QComboBox::drop-down {
            border: 0px;
            padding-right: 5px;
        }
        QComboBox QAbstractItemView {
            outline: 0px;
            background-color: #ffffff;
            selection-background-color: #a6cfff;
        }
    )");

    confirmButton->setStyleSheet(R"(
        QPushButton {
            background-color: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #ff9999, stop:1 #ff6666);
            color: white;
            font-weight: bold;
            border-radius: 6px;
            padding: 8px;
        }
        QPushButton:hover {
            background-color: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #ffcccc, stop:1 #ff9999);
        }
    )");

    QHBoxLayout *coverLayout = new QHBoxLayout();
    coverLayout->addWidget(selectImageButton, 1);
    coverLayout->addWidget(tagComboBox, 1);
    coverLayout->addWidget(confirmButton, 2);
    mainLayout->addLayout(coverLayout);
}

void PreviewDialog::selectCoverImage()
{
    QString imagePath = QFileDialog::getOpenFileName(this, "选择封面图片", "", "Images (*.png *.jpg *.bmp)");
    if (!imagePath.isEmpty())
    {
        coverImage = QImage(imagePath);
        if (!coverImage.isNull())
        {
            coverImage = coverImage.scaled(CoverWidth, CoverHeight, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
            blogItem->setCover(QPixmap::fromImage(coverImage));
        }
    }
}

QImage PreviewDialog::getCoverImage() const
{
    return coverImage;
}

QString PreviewDialog::getSelectedTag() const
{
    QString selectedTagValue = tagComboBox->currentData().toString();
    return selectedTagValue;
}